package com.aitoedu.server.controller;

import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import com.aitoedu.server.constant.MessageConstant;
import com.aitoedu.server.dto.LoginDTO;
import com.aitoedu.server.entity.User;
import com.aitoedu.server.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/logic/user")
@Slf4j
@CrossOrigin
@Tag(name = "所有用户相关接口")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/login")
    @Operation(summary = "登录")
    public SaResult login(@RequestBody LoginDTO loginDTO){
        log.info("登录：{}",loginDTO);
        User user = userService.login(loginDTO);
        StpUtil.login(user.getId());
        return SaResult.ok(MessageConstant.LOG_SUCCESS);
    }

    @GetMapping("logout")
    @Operation(summary = "退出登录")
    public SaResult logout(){
        log.info("退出登录：{}",StpUtil.getTokenInfo());
        StpUtil.logout();
        return SaResult.ok(MessageConstant.LOGOUT);
    }

}
